package com.ellecity06.common.ktx

import android.os.SystemClock
import android.view.View
import com.ellecity06.common.R
import com.ellecity06.common.listener.DebounceListener

/**
 * @author ellecity06
 * @time 2023/4/15 16:54
 * @des
 */
private var View.clickLastTime: Long
    get() = getTag(R.id.clickLastTimeKey) as? Long ?: 0
    set(value) = setTag(R.id.clickLastTimeKey, value)

fun View.onDebounceClick(block: (View) -> Unit) {
    setOnClickListener {
        val currentTime = SystemClock.elapsedRealtime()
        if (currentTime - clickLastTime >= 500) {
            clickLastTime = currentTime
            block.invoke(it)
        }
    }
}

fun View.setClickListener(listener: DebounceListener) {
    setOnClickListener(listener)
}
